package com.huixue.cn.ztej.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.huixue.cn.ztej.base.Constant;
import com.huixue.cn.ztej.bean.EmployApply;
import com.huixue.cn.ztej.bean.Project;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.impl.EmployApplyServiceImpl;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;

@Domain(EmployApply.class)
@Controller
@RequestMapping("/sys/apply")
public class EmployApplyController extends BaseController {
	@Autowired
	private EmployApplyServiceImpl employApplyServiceImpl;
	
	@Autowired
	private CommonsMultipartResolver multipartResolver;
	/**
	*方法描述：跳转到列表页面
	*创建人：fym
	*创建时间：2017年1月10日 17:18:33
	**/
	@RequestMapping("/toSearch")
	public String toListtemployapply(HttpServletResponse response,HttpServletRequest request,Model model){
		User user = new User();
		user.setRoleId("2");
		model.addAttribute("user",user);
		List<EmployApply> list = new ArrayList<EmployApply>();
		if(user.getRoleId().equals("2")){ //工程人员查询
			Map<String, Object> map = new HashMap<String, Object>();
			//list = employApplyServiceImpl.searchByPage(map,1.10);
			List<Integer> list1 = new ArrayList<Integer>();
			list1.add(0);
			list1.add(1);
			
			map.put("state", list1);
			Results results = employApplyServiceImpl.searchByPage(map, 0, 20);
			model.addAttribute("res",results);
		}
		
		return "/employ/temployapply/list";
	}

	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到添加页面")
	@RequestMapping(value = "/toAdd", method = RequestMethod.GET)
	public String toAdd(Model model) {
		User user = new User();
		user.setRoleId("2");
		user.setUser_id(2);
		user.setUserName("龚东阳");
		model.addAttribute("user",user);
		return "/employ/temployapply/add";
		
	}
	
	/**
	 * 增加用工申请
	 * @param sysProInfo
	 * @return
	 */
	public Results add(HttpServletRequest request,EmployApply employApply){
		Results results = null;
		employApply.setApplyTime(new Date());
		int applyId = employApplyServiceImpl.insert(employApply);

		results = new Results(API_RETURN_STATUS.NORMAL.value(), "用工申请保存成功！");
		return results; 
	}
	
	/**
	 * 上传文件
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public boolean upload(String templateId,HttpServletRequest request, HttpServletResponse response)
			throws IllegalStateException, IOException {
		String ss = Constant.UPLOAD_FILE_WIN;
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipart = multipartResolver;
		// 判断 request 是否有文件上传,即多部分请求
		if (multipart.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			int i = 1;
			while (iter.hasNext()) {
				String path = ss + File.separator;
				// 取得上传文件
				MultipartFile file = multiRequest.getFile(iter.next());
				String name = file.getName();
				if (file != null) {
					// 取得当前上传文件的文件名称
					String myFileName = file.getOriginalFilename();
					// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
					// if(myFileName.trim() !=""){
					if (StringUtils.isNotBlank(myFileName.trim())) {
						// 重命名上传后的文件名
						String[] sp = myFileName.split("\\.");
						String fileName = name + "." + sp[sp.length - 1];
						// 定义上传路径
						String filePath = path + fileName;
						File localFile = new File(filePath);
						if(i==1){
							String littlePath =ss + File.separator;
							i++;
						}
						file.transferTo(localFile);
					}
				}
			}
		}
		return true;
	}

}
